package org.xs.big_event.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.xs.big_event.dao.class_table.MenuEntry;
import org.xs.big_event.service.IMenuConfigService;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Service
@Data
public class MenuConfigService implements IMenuConfigService {
    private String path;
    public ArrayList<Map<String, MenuEntry>> loadMenuConfig() {
        try {
            // 从类路径加载文件 src/main/resources/Class.json
            ClassPathResource resource = new ClassPathResource(path);
            InputStream inputStream = resource.getInputStream();
            // 解析 JSON 到 Java 对象
            ObjectMapper objectMapper = new ObjectMapper();
            // 解析 JSON 到 Map
            Map<String, MenuEntry> menuMap=new HashMap<>();
            ArrayList<Map<String, MenuEntry>> menuList = objectMapper.readValue(
                    inputStream,
                    new TypeReference<ArrayList<Map<String, MenuEntry>>>() {}
            );

            return menuList;

        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>(); // 返回空Map而不是null
        }
    }
}